/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.resource.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.resource.domain.Venu;
import com.jeeplus.resource.service.VenuService;
import com.jeeplus.resource.service.dto.VenuApplyDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.resource.domain.VenueStatus;
import com.jeeplus.resource.service.dto.VenueStatusDTO;
import com.jeeplus.resource.service.mapstruct.VenueStatusWrapper;
import com.jeeplus.resource.service.VenueStatusService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 场地状态Controller
 * @author 淋漓
 * @version 2024-08-21
 */

@Api(tags ="场地状态")
@RestController
@RequestMapping(value = "/resource/venueStatus")
public class VenueStatusController {

	@Autowired
	private VenueStatusService venueStatusService;

	@Autowired
	private VenueStatusWrapper venueStatusWrapper;

	@Autowired
	private VenuService venueService;

	/**
	 * 查看场地状态列表数据
	 */
	@ApiLog("查询查看场地状态列表数据")
	@ApiOperation(value = "查询查看场地状态列表数据")
	@PreAuthorize("hasAuthority('resource:venueStatus:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<VenueStatusDTO>> list(VenueStatusDTO venueStatusDTO, Page<VenueStatus> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (venueStatusDTO, VenueStatusDTO.class);
		if(venueStatusDTO.getVenueName()!=null && !venueStatusDTO.getVenueName().isEmpty()){
			Venu venu = venueService.getOne(new QueryWrapper<Venu>()
					.eq("venue_name", venueStatusDTO.getVenueName()));
			if(venu!=null){
				queryWrapper.eq("venue_id", venu.getId());
			}
			else {
				return ResponseEntity.ok (new Page<>());
			}
		}
		if(venueStatusDTO.getMyCreateTime()!=null && !venueStatusDTO.getMyCreateTime().isEmpty()){
			queryWrapper.like("date", venueStatusDTO.getMyCreateTime());
		}
		else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
			queryWrapper.gt("date",sdf.format(new Date()));
		}
		IPage<VenueStatusDTO> result = venueStatusWrapper.toDTO ( venueStatusService.page (page, queryWrapper) );
		List<VenueStatusDTO> records = result.getRecords();
		setVenueName(records);
		setLevel(records);
		return ResponseEntity.ok (result);
	}



	/**
	 * 根据Id获取查看场地状态数据
	 */
	@ApiLog("根据Id获取查看场地状态数据")
	@ApiOperation(value = "根据Id获取查看场地状态数据")
	@PreAuthorize("hasAnyAuthority('resource:venueStatus:view','resource:venueStatus:add','resource:venueStatus:edit')")
	@GetMapping("queryById")
	public ResponseEntity<VenueStatusDTO> queryById(String id) {
		return ResponseEntity.ok ( venueStatusWrapper.toDTO ( venueStatusService.getById ( id ) ) );
	}

	/**
	 * 保存查看场地状态
	 */
	@ApiLog("保存查看场地状态")
	@ApiOperation(value = "保存查看场地状态")
	@PreAuthorize("hasAnyAuthority('resource:venueStatus:add','resource:venueStatus:edit')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody VenueStatusDTO venueStatusDTO) {
		//新增或编辑表单保存
		venueStatusService.saveOrUpdate (venueStatusWrapper.toEntity (venueStatusDTO));
        return ResponseEntity.ok ( "保存查看场地状态成功" );
	}


	/**
	 * 删除查看场地状态
	 */
	@ApiLog("删除查看场地状态")
	@ApiOperation(value = "删除查看场地状态")
	@PreAuthorize("hasAuthority('resource:venueStatus:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        venueStatusService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除查看场地状态成功" );
	}

    /**
     * 导出查看场地状态数据
     *
     * @param venueStatusDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出查看场地状态数据")
    @PreAuthorize("hasAnyAuthority('resource:venueStatus:export')")
    @GetMapping("export")
    public void exportFile(VenueStatusDTO venueStatusDTO, Page <VenueStatus> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (venueStatusDTO, VenueStatusDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据

        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List < VenueStatus> result = venueStatusService.page ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( venueStatusService, venueStatusWrapper ).exportExcel ( result,  options.getSheetName ( ), VenueStatusDTO.class, fileName,options.getExportFields (), response );
    }

    /**
     * 导入查看场地状态数据
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('resource:venueStatus:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( venueStatusService, venueStatusWrapper ).importExcel ( file, VenueStatusDTO.class );
        return ResponseEntity.ok ( result );
    }

    /**
     * 下载导入查看场地状态数据模板
     *
     * @param response
     * @return
     */
    @PreAuthorize ("hasAnyAuthority('resource:venueStatus:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "查看场地状态数据导入模板.xlsx";
        List<VenueStatusDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( venueStatusService, venueStatusWrapper ).exportExcel ( list,  "查看场地状态数据", VenueStatusDTO.class, fileName, null, response );
    }

	/**
	 * 设置场地名
	 * @param records
	 */
	public void setVenueName(List<VenueStatusDTO> records){
		for(VenueStatusDTO record:records) {
			if (record != null) {
				record.setVenueName(venueService.getById (record.getVenueId()).getVenueName());
			}
		}
	}

	/**
	 * 设置状态颜色
	 * @param records
	 */
	public void setLevel(List<VenueStatusDTO> records){
		for (VenueStatusDTO record : records) {
			if(record!=null) {
				if ("已占用".equals(record.getAm1())) {
					record.setLevel("danger");
				} else {
					record.setLevel("success");
				}
			}
		}
	}
}
